﻿using Common.Attributes;
using MODEL;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
    public  class SysController:Controller
    {
        #region 权限列表 视图 +  Permission()
        /// <summary>
        /// 权限列表 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Permission()
        {
            return View();
        } 
        #endregion 

        #region 子权限列表 视图 + PermissionSon()
        /// <summary>
        /// 子权限列表 视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PermissionSon()
        {
            int parenentMenuId = int.Parse(Request.QueryString["PId"]);
            return View();
        } 
        #endregion

        #region 1.1 权限列表 数据 +GetPermData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 权限列表 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPermData()
        {  
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.GetPagedList(pageIndex, pageSize, x => x.PId != null, m => m.PId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.GetListBy(x => x.PId!=null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 1.1 子权限列表 数据 +PermissionSon()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        ///  子权限列表 数据
        /// </summary>
        /// <returns></returns>
        public ActionResult PermissionSon(FormCollection form)
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //获取父权限id
            int parenentMenuId = int.Parse(Request.QueryString["PId"]);

            var list = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.GetPagedList(pageIndex, pageSize, x => x.PParent == parenentMenuId, m => m.PId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.GetListBy(x => x.PId!=null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改权限 window 的html + EditPermission()
        /// <summary>
        /// 加载修改权限 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditPermission(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.GetListBy(x => x.PId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        } 
        #endregion

        #region post修改权限 EditPermission(MODEL.ViewModel.Permisson model)
        /// <summary>
        ///post修改权限 EditPermission(MODEL.ViewModel.Permisson model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditPermission(MODEL.qgzx_permission model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.Modify(model, "PName", "PAreaName", "PControllerName", "PActionName", "PRemark");
            if (res > 0)
            {
                return OperateContext.Current.RedirectAjax("nologin", "您还没有登陆或没有权限", null, "/admin/sys/permission");
                return Redirect("/admin/sys/permission");
            }
            return Redirect("/admin/sys/permission");
        }
        #endregion

        #region 在这新增权限 html代码 + AddPermission()
        /// <summary>
        /// 在这新增权限 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddPermission()
        {
            return PartialView("EditPermission");  
        } 
        #endregion

        #region 在这新增权限 html代码 + AddPermission(MODEL.qgzx_permission model)
        /// <summary>
        /// 在这新增权限 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddPermission(MODEL.qgzx_permission model)
        {
            model.PAddTime = DateTime.Now;

            int res = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/sys/permission?ok");
            }
            return Redirect("/admin/sys/permission?err");
        } 
        #endregion

        #region 删除权限 + DelPermission()
        /// <summary>
        /// 删除权限 + DelPermission()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelPermission()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_permissionBLL.DelBy(x => x.PId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {
                
                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限", null, "");
            }
        } 
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string PName = Request.QueryString["PName"];
            string PAreaName = Request.QueryString["PAreaName"];
            string PControllerName = Request.QueryString["PControllerName"];
            string PActionName = Request.QueryString["PActionName"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_permissionBLL.GetListBy(x => x.PId != null).ToList();
            if (PName != "")
            {
                list = list.Where(x => x.PName.Contains(PName)).ToList();
            }
            if (PAreaName != "")
            {
                list = list.Where(x => x.PAreaName.Contains(PAreaName)).ToList();
            }
            if (PControllerName != "")
            {
                list = list.Where(x => x.PControllerName.Contains(PControllerName)).ToList();
            }
            if (PActionName != "")
            {
                list = list.Where(x => x.PActionName.Contains(PActionName)).ToList();
            }
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        } 
        #endregion
    }
}
